<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="fn('')">添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          default-expand-all
          :data="data"
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ node,data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="fn(data.id)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="fn2(data.id)">查询子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog
      :title="isEdit?'编辑部门':'添加部门'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hDialogClose"
    > <deptDiglog
      v-if="showDialog"
      ref="deptDialog"
      :is-edit="isEdit"
      :origin-list="originList"
      :pid="curId"
      @success="success"
      @close="hClose"
    /> </el-dialog>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/department'
import { treeListData } from '@/utils/index'
import deptDiglog from '@/views/departments/deptDialog.vue'

export default {
  components: { deptDiglog },
  data() {
    return {
      originList: [],
      isEdit: false,
      curId: '',
      showDialog: false,
      data: [

        {
          label: '总裁办'
        },
        {
          label: '财务部',
          children: [
            {
              label: '核算部'
            },
            {
              label: '成本部'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.doDepartment()
  },
  methods: {
    hDialogClose() {
      this.$refs.deptDialog.resetForm()
    },
    hClose() {
      this.showDialog = false
    },
    success() {
      this.showDialog = false
      this.doDepartment()
      // alert('知道了')
    },
    fn(id) { // 添加
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    fn2(id) { // 修改
      this.curId = id
      this.isEdit = true
      this.showDialog = true
    //  this.$nextTick(()=>{
    //       console.log('获取子组件的引用', this.$refs.deptDialog)
    //       console.log('当前的curId', id)
    //       console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.pid)//获取到的是undfind
    //       this.$refs.deptDialog.loadEmployeeSimple()//调用
    //  })
    },
    hDel(id) {
      this.$confirm('你确定要离开吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
        this.$message('删除成功')
        this.doDepartment()
      }).catch(() => {})
    },
    async doDel(id) {
      await delDepartment(id)
    },
    async doDepartment() {
      const res = await getDepartment()
      res.data.depts.shift()
      console.log('res.data.depts', res.data.depts)
      this.data = treeListData(res.data.depts)
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          name: item.name,
          pid: item.pid,
          code: item.code
        }
      })
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>
